""" Replacing netbiosname with hostname_virtual as needed

Revision ID: 3df90537bffa
Revises: b690e5ae986d
Create Date: 2023-03-15 15:59:58.605407+00:00

"""
from alembic import op
import sqlalchemy as sa
import re


# revision identifiers, used by Alembic.
revision = '3df90537bffa'
down_revision = 'b690e5ae986d'
branch_labels = None
depends_on = None

RE_NETBIOSNAME = re.compile(r"^[a-zA-Z0-9\.\-_!@#\$%^&\(\)'\{\}~]{1,15}$")

def upgrade():
    conn = op.get_bind()

    ngc = dict(conn.execute('SELECT * FROM network_globalconfiguration').fetchone())
    if not ngc['gc_hostname_virtual']:
        return

    if not RE_NETBIOSNAME.match(ngc['gc_hostname_virtual']):
        return

    conn.execute('UPDATE services_cifs SET cifs_srv_netbiosname = ?', ngc['gc_hostname_virtual'])

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    pass
    # ### end Alembic commands ###
